package com.carson.undergo.ui.fragment

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.carson.undergo.R
import com.carson.undergo.databinding.FragmentMineBinding
import com.model.common.base.activity.BaseActivity
import com.model.common.base.fragment.BaseFragment
import com.model.common.utils.always.ToastUtils
import com.model.common.utils.function.version.CheckVersionUtils
import com.model.common.utils.function.version.VersionUtils
import com.model.common.utils.image.GlideUtils

/**
 * Create by Carson on 2021/12/24.
 * 个人中心
 */
class MineFragment : BaseFragment<FragmentMineBinding>() {

    override fun getViewBinding(inflater: LayoutInflater, viewGroup: ViewGroup):
            FragmentMineBinding =
        FragmentMineBinding.inflate(inflater, viewGroup, false)

    @SuppressLint("SetTextI18n")
    override fun initViewsAndEvents() {
        GlideUtils.circleImg(binding.imageHeader, R.drawable.icon_user)
        binding.tvVersion.text = "当前版本:${BaseActivity.getVersionName(mContext)}"
    }

    override fun addListener() {
        binding.tvVersion.setOnClickListener(this)
    }

    override fun onWidgetClick(view: View?) {
        if (view?.id == R.id.tv_version) {
            checkVersion()
        }
    }

    private fun checkVersion() {
        val desInfo = ArrayList<String>()
        desInfo.add("修复部分已知问题")
        desInfo.add("优化部分UI")
        val weChatUrl =
            "https://gdown.baidu.com/data/wisegame/4c625516bd55f355/ff1f4c625516bd55f355200829321579.apk"
        //比较新版本和旧版本，检查是否需要更新
        val isUpdate =
            VersionUtils.compareVersions("1.0.0.6", BaseActivity.getVersionName(mContext))
        if (isUpdate) {
            val utils = CheckVersionUtils(mContext, weChatUrl, desInfo, "1.0.0.6")
            utils.showUpdateVersion()
        } else {
            ToastUtils.showMessage(mContext, "已经是最新版本了哦~")
        }
    }
}